package dm.ast;

import test.envset.GlobalState;

import java.util.ArrayList;
import java.util.List;

/**
 * @author weihuixin
 * @date 2024/4/7
 * @Description
 */
public class DmQuery extends DmASTNode{

    public final static int INDEX_UNION = 0;
    public final static int INDEX_SELECT = 1;

    public List<DmSelect> querySelects = null;

    public final static int SUBCLAUSE_CNT = 2;
    public DmASTNode[] subClause = new DmASTNode[SUBCLAUSE_CNT];

    public DmQuery(){
        querySelects = new ArrayList<>();
        for(int i=0;i<SUBCLAUSE_CNT;i++){
            subClause[i] = null;
        }
    }

    public void setUnion(DmUnionQuery dmUnionQuery){
        subClause[INDEX_UNION] = dmUnionQuery;
        for(DmSelect s:dmUnionQuery.Selects){
            querySelects.add(s);
        }
    }

    public void setSelect(DmSelect dmSelect){
        subClause[INDEX_SELECT] = dmSelect;
        querySelects.add(dmSelect);
    }

    public String toSql(){
        String sql = "";
        if(subClause[INDEX_UNION] !=null){
            DmUnionQuery dmUnionQuery =(DmUnionQuery) subClause[INDEX_UNION];
            sql = dmUnionQuery.toSql();
            return sql;
        }else if(subClause[INDEX_SELECT] != null){
            DmSelect dmSelect = (DmSelect) subClause[INDEX_SELECT];
            sql = dmSelect.toSql();
            return sql;
        }
        return sql;
    }

    public void fill(GlobalState globalState){
        if(subClause[INDEX_UNION] !=null){
            DmUnionQuery dmUnionQuery =(DmUnionQuery) subClause[INDEX_UNION];
            dmUnionQuery.fill(globalState);
        }else if(subClause[INDEX_SELECT] != null){
            DmSelect dmSelect = (DmSelect) subClause[INDEX_SELECT];
            dmSelect.fill(globalState);
        }
    }

    @Override
    public void setWidt() {

    }

    @Override
    public void setDept() {

    }

    @Override
    public DmDataType getExpressionType() {
        return null;
    }
}
